//
//  WPTeamTransAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/9.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPTeamTransAlertView.h"
@interface WPTeamTransAlertView ()
@property (nonatomic, strong) UIScrollView * backScrollView;/**< 背景滚动视图 */
@property (nonatomic, strong) UILabel * positionIntroLabel;/**< 票号标题 */
@property (nonatomic, strong) UILabel * detailPositionLabel;/**< 详细票号 */
@property (nonatomic, strong) UILabel * buyInTitleLabel;/**< 买入价格标题 */
@property (nonatomic, strong) UILabel * buyInPriceLabel;/**< 买入价格 */
@property (nonatomic, strong) UILabel * sellPriceIntroLabel;/**< 转卖价格标题 */
@property (nonatomic, strong) UITextField * sellPriceTextField;/**< 转卖价输入框 */
@property (nonatomic, strong) UIButton * sureBtn;/**< 确定按钮 */
@property (nonatomic, strong) NSArray * dataSource;/**< 数据源 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */

@end
@implementation WPTeamTransAlertView
+ (WPTeamTransAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithDatasource:(NSArray *)dataSource andWithComplete:(void (^)(NSString *))complete {
    WPTeamTransAlertView * view = [[WPTeamTransAlertView alloc]initWithTitle:title withSize:rect withDataSource:dataSource WithComplete:complete];
    [view show];
    return view;
}
- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect withDataSource:(NSArray *)arr WithComplete:(void (^)(NSString *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _complete = complete;
        _dataSource = arr;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.positionIntroLabel];
    [self addSubview:self.detailPositionLabel];
    [self addSubview:self.buyInTitleLabel];
    [self addSubview:self.buyInPriceLabel];
    [self addSubview:self.sellPriceIntroLabel];
    [self addSubview:self.sellPriceTextField];
    [self addSubview:self.sureBtn];
}
#pragma mark - responds events
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}
- (void)respondsToSurnBtn {
    [self hide];
    _complete(_sellPriceTextField.text);
}
#pragma mark - getter
- (UILabel *)positionIntroLabel {
    if (!_positionIntroLabel) {
        _positionIntroLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(25*DHFlexibleHorizontalMutiplier(), 50*DHFlexibleHorizontalMutiplier(),50*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"票号：";
            lab;
        });
    }
    return _positionIntroLabel;
}
- (UILabel *)detailPositionLabel {
    if (!_detailPositionLabel) {
        _detailPositionLabel = ({
            NSMutableString * str = [NSMutableString stringWithString:_dataSource[0]];
            for (int i = 1; i < _dataSource.count; i++) {
                if (i % 3 == 0) {
                    [str appendString:@"\n"];
                    [str appendString:_dataSource[i]];
                    continue;
                }
                [str appendString:[NSString stringWithFormat:@"  %@  ", _dataSource[i]]];

            }
            CGRect rect = [WPCalculateRectHandle calculateSizeWithFont:14*DHFlexibleHorizontalMutiplier() Text:str Width:170*DHFlexibleHorizontalMutiplier()];
            NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc]initWithString: str];
            NSMutableParagraphStyle *warnParagraph = [[NSMutableParagraphStyle alloc] init];
            warnParagraph.lineSpacing = 5*DHFlexibleHorizontalMutiplier();//行间距
            [attributedStr addAttribute:NSParagraphStyleAttributeName value:warnParagraph range:NSMakeRange(0, str.length)];
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.positionIntroLabel.frame), CGRectGetMinY(self.positionIntroLabel.frame), 170*DHFlexibleHorizontalMutiplier(), rect.size.height)];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.numberOfLines = 0;
            lab.attributedText = attributedStr;
            lab;
        });
    }
    return _detailPositionLabel;
}
- (UILabel *)buyInTitleLabel {
    if (!_buyInTitleLabel) {
        _buyInTitleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.positionIntroLabel.frame), CGRectGetMaxY(self.detailPositionLabel.frame), 80*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"买入价格";
            lab;
        });
    }
    return _buyInTitleLabel;
}
- (UILabel *)buyInPriceLabel {
    if (!_buyInPriceLabel) {
        _buyInPriceLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.buyInTitleLabel.frame), CGRectGetMaxY(self.detailPositionLabel.frame), 80*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"800";
            lab;
        });
    }
    return _buyInPriceLabel;
}
- (UILabel *)sellPriceIntroLabel {
    if (!_sellPriceIntroLabel) {
        _sellPriceIntroLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.positionIntroLabel.frame), CGRectGetMaxY(self.buyInPriceLabel.frame), 80*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:13*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(102, 102, 102, 1);
            lab.text = @"转卖价格";
            lab;
        });
    }
    return _sellPriceIntroLabel;
}
- (UITextField *)sellPriceTextField {
    if (!_sellPriceTextField) {
        _sellPriceTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.sellPriceIntroLabel.frame), CGRectGetMinY(self.sellPriceIntroLabel.frame), 140*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier())];
            
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(102, 102, 102, 1);
            text.keyboardType = UIKeyboardTypeDecimalPad;
            text.clearButtonMode = UITextFieldViewModeWhileEditing;
            text.backgroundColor = BGCOLOR;
            text.placeholder = @"请输入转卖价格";
            text;
        });
    }
    return _sellPriceTextField;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), 220*DHFlexibleHorizontalMutiplier(), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"确 定" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToSurnBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _sureBtn;
}

@end
